//
//  MedicineListm.m
//  EcmDoctor
//
//  Created by Mac on 16/3/2.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "MedicineListm.h"
#import "CureInfom.h"
@implementation MedicineListm

+(void)judgeIsUpdateDrugInfoYBWithmaxSid:(NSString*)maxSid success:(void (^)(NetWorkBaseResponse *))success failure:(void (^)(NSError *))failure
{
    
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];
 
      req.moduleName = @"DoctorRS";
    req.methodName = @"judgeIsUpdateDrugInfoYB";
    [req setValue:maxSid forKey:@"maxSid"];
   
    
    
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
        
        if (responseObject.code.integerValue == 1) {
            if (!responseObject.data) {
                success(nil);
                return;
            }
            NSLog(@"--------------%@",responseObject);
           
            if (error) {
                failure(error);
                return;
            }
            success(responseObject);
            
        }
        else{
            error = [NSError errorWithDomain:responseObject.msg code:responseObject.code.integerValue userInfo:nil];
            failure(error);
        }
        
    } failure:^(NSError *error) {
        failure(error);
        
    } cache:NO];
    
    

}
+(void)getDrugInfoYBwithclientDrugInfo:(NSArray *)clientDrugInfoAry success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure
{
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];

   if(clientDrugInfoAry.count==0)
    [req setValue:@"0"forKey:@"clientDrugInfo"];
    else
    {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:clientDrugInfoAry options:NSJSONWritingPrettyPrinted error:nil];
        NSString *str=[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

       [req setValue:str forKey:@"clientDrugInfo"];
    }
    req.moduleName = @"DoctorRS";
    req.methodName = @"getDrugInfoYB";
    
    
    
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
       
        if (responseObject.code.integerValue == 1) {
            if (!responseObject.data) {
                success(nil);
                return;
            }
           
            NSData *data = [responseObject.data dataUsingEncoding:NSUTF8StringEncoding];
             NSArray *userAttrs = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

            if (error) {
                failure(error);
                return;
            }
            NSMutableArray *_indexArr=[NSMutableArray array];
            
            for (int i = 'A'; i <= 'Z'; i ++) {
        
                NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];
              [_indexArr addObject:arr];
            }

            NSUserDefaults *doctorUser=[NSUserDefaults standardUserDefaults];
            NSMutableArray *updateAry=[NSMutableArray array];
            for(int i=0;i<userAttrs.count;i++)
            {
                MedicineListm *medicineListm=[[MedicineListm alloc]initWithDictionary:userAttrs[i]];
                if(i==0)
                {[doctorUser setObject:medicineListm.maxSid forKey:@"maxSid"];
                [doctorUser synchronize];
                }
                for (int j = 'A'; j <= 'Z'; j ++)
                {
                 if([[medicineListm.spelling substringToIndex:1]isEqualToString:[NSString stringWithFormat:@"%c",j]])
                    [_indexArr[j-'A'] addObject:medicineListm];
                }
                NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:medicineListm.drug_sid,@"drug_sid",medicineListm.version,@"version", nil];
                CureInfom *cureinfom=[[CureInfom alloc]initWithDictionary:dic];
                [updateAry addObject:cureinfom];
                
            }
            
            NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
            //文件管理器
            NSFileManager *fileManager = [NSFileManager defaultManager];
             //  [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
            [fileManager removeItemAtPath:[DocumentsPath stringByAppendingString:@"/clientDrugInfo"] error:nil];
            [fileManager removeItemAtPath:[DocumentsPath stringByAppendingString:@"/cureinfom"] error:nil];
       
            NSData *data2 = [NSKeyedArchiver archivedDataWithRootObject:updateAry];
           [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/cureinfom"] contents:data2 attributes:nil];
            NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:_indexArr];
         
            [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/clientDrugInfo"] contents:data1 attributes:nil];
            
            
            
  
            success(_indexArr);

            
        }
        else{
            error = [NSError errorWithDomain:responseObject.msg code:responseObject.code.integerValue userInfo:nil];
            failure(error);
        }
        
    } failure:^(NSError *error) {
        failure(error);
        
    } cache:NO];

}
@end
